﻿using System;
using System.Reflection;
using System.Web;
using System.Web.Hosting;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using MyAMS.Utility;

namespace MyAMS.WebServer
{
    internal static class ContainerFactory
    {
        internal static IUnityContainer Create()
        {
            IUnityContainer parentContainer = new UnityContainer();

            parentContainer
                .RegisterInstance(HostingEnvironment.VirtualPathProvider)
                .RegisterInstance("RegisterRoutesHandler", typeof(MvcRouteHandler));

            parentContainer
                .RegisterType<IComponent, ModulesManager>("Component.ModulesManager")
                .RegisterType<IEnvironmentLoader, EnvironmentLoader>();

            string configFilePath = String.Format(IUnityContainerExtensions.unityConfigurationFilePath, Assembly.GetExecutingAssembly().FullName.Split(',')[0]);
            return parentContainer.Configure(HttpContext.Current.Server.MapPath(configFilePath));
        }
    }
}
